package com.trex.modules.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.trex.modules.tbl.entity.Body;
import com.trex.modules.tbl.entity.Detail;
import com.trex.modules.tbl.entity.ParsedData;
import com.trex.modules.tbl.entity.SysInfo;

import java.io.IOException;
import java.util.*;

public class JsonToMapParser {

    public static List<Map<String, Object>> parseJsonToTable(String jsonString) {
        List<Map<String, Object>> result = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            JsonNode root = objectMapper.readTree(jsonString);
            JsonNode tablesNode = root.path("Tables");

            if (tablesNode.isArray()) {
                for (JsonNode tableNode : tablesNode) {
                    JsonNode columnsNode = tableNode.path("Columns");
                    JsonNode rowsNode = tableNode.path("Rows");

                    List<String> columnNames = new ArrayList<>();
                    for (JsonNode columnNode : columnsNode) {
                        columnNames.add(columnNode.path("Name").asText());
                    }

                    for (JsonNode rowNode : rowsNode) {
                        Map<String, Object> rowMap = new HashMap<>();
                        for (int i = 0; i < columnNames.size(); i++) {
                            String key = columnNames.get(i);
                            JsonNode valueNode = rowNode.get(i);
                            Object value = valueNode.isNull() ? null : valueNode.asText();
                            rowMap.put(key, value);
                        }
                        result.add(rowMap);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String json = "{\n" +
                "    \"SysInfo\": {\n" +
                "        \"Flag\": 0,\n" +
                "        \"Msg\": \"\",\n" +
                "        \"SvcName\": \"M2NE45\",\n" +
                "        \"UUID\": \"99a8377066b04616881e51dc7e79524d\"\n" +
                "    },\n" +
                "    \"Tables\": {\n" +
                "        \"BODY\": [\n" +
                "            {\n" +
                "                \"OPERATION_FLAG\": \"I\",\n" +
                "                \"FLAG\": \"1\",\n" +
                "                \"STATUS\": \"1\",\n" +
                "                \"STATION_USER_CODE\": \"1123\",\n" +
                "                \"STATION_USER_NAME\": \"LISHI\",\n" +
                "                \"STATION_TIME\": \"20240902160000\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"DETAIL\": [\n" +
                "            {\n" +
                "                \"REC_ID\": \"123\",\n" +
                "                \"WAGONNO\": \"4124653\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"REC_ID\": \"124\",\n" +
                "                \"WAGONNO\": \"4124654\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";

        ObjectMapper objectMapper = new ObjectMapper();
        ParsedData parsedData = new ParsedData();

        try {
            JsonNode rootNode = objectMapper.readTree(json);

            // 解析 SysInfo
            JsonNode sysInfoNode = rootNode.path("SysInfo");
            SysInfo sysInfo = new SysInfo();
            sysInfo.setFlag(sysInfoNode.path("Flag").asInt());
            sysInfo.setMsg(sysInfoNode.path("Msg").asText());
            sysInfo.setSvcName(sysInfoNode.path("SvcName").asText());
            sysInfo.setUuid(sysInfoNode.path("UUID").asText());
            parsedData.setSysInfo(sysInfo);

            // 解析 BODY 列表
            JsonNode bodyArray = rootNode.path("Tables").path("BODY");
            List<Body> bodyList = new ArrayList<>();
            for (JsonNode bodyNode : bodyArray) {
                Body body = new Body();
                body.setOperationFlag(bodyNode.path("OPERATION_FLAG").asText());
                body.setFlag(bodyNode.path("FLAG").asText());
                body.setStatus(bodyNode.path("STATUS").asText());
                body.setStationUserCode(bodyNode.path("STATION_USER_CODE").asText());
                body.setStationUserName(bodyNode.path("STATION_USER_NAME").asText());
                body.setStationTime(bodyNode.path("STATION_TIME").asText());
                bodyList.add(body);
            }
            parsedData.setBodyList(bodyList);

            // 解析 DETAIL 列表
            JsonNode detailArray = rootNode.path("Tables").path("DETAIL");
            List<Detail> detailList = new ArrayList<>();
            for (JsonNode detailNode : detailArray) {
                Detail detail = new Detail();
                detail.setRecId(detailNode.path("REC_ID").asText());
                detail.setWagonNo(detailNode.path("WAGONNO").asText());
                detailList.add(detail);
            }
            parsedData.setDetailList(detailList);

            // 输出解析后的数据
            System.out.println(parsedData);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }


